/**
 * 角色管理的单例
 */
var Role = {
    id: "roleTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
Role.initColumn = function () {
    var columns = [
        {field: 'selectItem', radio: true},
        {title: '名称', field: 'name', align: 'center', valign: 'middle', sortable: true},
        {title: '编码', field: 'code', align: 'center', valign: 'middle', sortable: true},
        {title: '上级角色', field: 'parentName', align: 'center', valign: 'middle', sortable: true},
        {title: '描述', field: 'description', align: 'center', valign: 'middle', sortable: true},
        {title: '操作', field: 'id', align: 'center', width: '120px', valign: 'middle',formatter:function(value,row,index){
    		var _html = '<a href="javascript:Role.edit(\''+row.id+'\')" title="修改"><i class="fa fa-pencil"></i></a>';
    		_html += '<a href="javascript:Role.del(\''+row.id+'\',\''+row.name+'\')" title="删除"><i class="fa fa-trash-o"></i></a>';
    		return _html;
        }}]
    return columns;
};


/**
 * 检查是否选中
 */
Role.check = function () {
    var selected = $('#' + this.id).bootstrapTreeTable('getSelections');
    if (selected.length == 0) {
        Sys.info("请先选中表格中的某一记录！");
        return false;
    } else {
        Role.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加角色
 */
Role.add = function () {
	this.layerIndex = Sys.open('新增角色',Sys.backendPath + '/role/role_add',['800px', '450px']);
};

/**
 * 点击修改按钮时
 */
Role.edit = function (id) {
	var flag = false;
	if(id){
		flag = true;
	}else if (this.check()) {
        id = this.seItem.id;
        flag = true;
    }
	if (flag) {
		this.layerIndex = Sys.open('修改角色',Sys.backendPath + '/role/role_edit/' + id,['800px', '450px']);
    }
};

/**
 * 删除角色
 */
Role.del = function (id,name) {
	var flag = false;
	if(id){
		flag = true;
	}else if (this.check()) {
        id = this.seItem.id;
        name = this.seItem.name
        flag = true;
    }
	if (flag) {
        var operation = function(){
	        var ajax = new $ax(Sys.backendPath + "/role/remove", function (data) {
	            Sys.success("删除成功!");
	            Role.table.refresh();
	        }, function (data) {
	            Sys.error("删除失败!" + data.responseJSON.message + "!");
	        });
	        ajax.set("roleId", id);
	        ajax.start();
	    };
	    Sys.confirm("是否删除角色【" + name + "】?",operation);
    }
};

/**
 * 权限配置
 */
Role.assign = function (resourceGroup) {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '权限配置',
            area: ['300px', '450px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Sys.backendPath + '/role/role_assign/'+resourceGroup+'/' + this.seItem.id
        });
        this.layerIndex = index;
    }
};

/**
 * 搜索角色
 */
Role.search = function () {
    var queryData = {};
    queryData['roleName'] = $("#roleName").val();
    Role.table.refresh({query: queryData});
}

$(function () {
    var defaultColunms = Role.initColumn();
    var table = new BSTreeTable(Role.id, Sys.backendPath + "/role/list", defaultColunms);
    table.setExpandColumn(1);
    table.setIdField("id");
    table.setParentIdField("parentId");
    table.setExpandAll(true);
    table.init();
    Role.table = table;
});
